<%--
  Created by IntelliJ IDEA.
  User: Liston
  Date: 2018/12/8
  Time: 23:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <%
        pageContext.setAttribute("APP_PATH", request.getContextPath()); //teacher_science_system/
        //        pageContext.setAttribute("APP_PATH", "teacher_science_system/");
    %>
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</head>
<body>
<jsp:include page="checker_header.jsp"/>


<script type = "text/javascript">
    //得到检查结果

    //1、页面加载完成以后，直接去发送一个ajax请求，要到分页数据
    $(function(){
        //去首页
        to_page(1,"/selAllNewPaper");
    });

    function to_page(pn,road) {
        $.ajax({
            url:"${APP_PATH}"+road,
            data:"pn="+pn,
            type:"GET",
            success:function(result){
                //console.log(result);
                //1、解析并显示数据
                build_check_paper(result);
                //2、解析并显示分页数据
                build_page_info(result);
                //3、解析显示分页条数据
                build_page_nav(result,road);
            }
        });
    }

    //1、解析并显示数据
    function  build_check_paper(result) {
        //清空table表格
        $("#checkPaper_table tbody").empty();
        var  emps = result.data.list;
        $.each(emps,function (index,item) {
            var resultUUIdTd = $("<td></td>").append(item.resultuuid);
            var paperNameTd = $("<td></td>").append(item.papername);
            var firstNameTd = $("<td></td>").append(item.firstname);
            var allNameTd = $("<td></td>").append(item.allname);
            var paperTypeidTd = $("<td></td>").append(item.papertypeid);
            var checkResultTd = $("<td></td>");

            var supportFunTd = $("<td></td>").append(item.supportfun);

            var subBtn = $("<button></button>").addClass("btn btn-success btn-sm sub_btn")
                .append($("<span></span>").addClass("glyphicon glyphicon-ok"))
                .append("通过");
            subBtn.attr("id",item.resultuuid);
            var failBtn =$("<button></button>").addClass("btn btn-danger btn-sm fail_btn")
                .append($("<span></span>").addClass("glyphicon glyphicon-remove")
                    .append("拒绝"));
            failBtn.attr("id",item.resultuuid);
            $.ajax({
                url:"${APP_PATH}/checkPaperVail",
                // data:"pn="+pn,
                type:"GET",
                success:function(result){
                    if(result.data==true){
                        checkResultTd .append("成功");
                    } else if(result.data==false){
                        checkResultTd.append("失败");
                        subBtn.addClass("disabled");
                    }
                }
            });


            var btnTd = $("<td></td>").append(subBtn).append(" ").append(failBtn);
            $("<tr></tr>").append(resultUUIdTd).append(paperNameTd)
                .append(firstNameTd).append(allNameTd)
                .append(paperTypeidTd).append(supportFunTd).append(checkResultTd)
                .append(btnTd)
                .appendTo("#checkPaper_table tbody");

        });
    }
    //解析显示分页信息
    function build_page_info(result) {
        $("#page_info_area").empty();
        $("#page_info_area").append("当前第"+result.data.pageNum+
            "页，总共"+result.data.pages+"页，总共"+result.data.total +"条记录。");

    }
    //解析显示分页条，点击分页要能去下一页
    function build_page_nav(result,road) {
        $("#page_nav_area").empty();
        var ul = $("<ul></ul>").addClass("pagination");
        //构建元素
        var firstPageLi = $("<li></li>").append($("<a></a>").append("首页").attr("href","#"));
        var prePageLi = $("<li></li>").append($("<a></a>").append("&laquo;"));
        if(result.data.hasPreviousPage == false){
            firstPageLi.addClass("disabled");
            prePageLi.addClass("disabled");

        }else{
            //为元素添加点击翻页的事件
            firstPageLi.click(function(){
                to_page(1,road);
            });

        }prePageLi.click(function(){
            to_page(result.data.pageNum-1,road);
        });
        var nextPageLi = $("<li></li>").append($("<a></a>").append("&raquo;"));
        var lastPageLi = $("<li></li>").append($("<a></a>").append("末页").attr("href","#"));
        if(result.data.hasNextPage == false){
            nextPageLi.addClass("disabled");
            lastPageLi.addClass("disabled");
        }else{
            nextPageLi.click(function () {
                to_page(result.data.pageNum+1,road);
            });
            lastPageLi.click(function () {
                to_page(result.data.pages,road);
            });
        }


        //添加首页和前一页的提示
        ul.append(firstPageLi).append(prePageLi);
        //1,2,3,4,5,遍历给ul中添加页码提示
        $.each(result.data.navigatepageNums,function (index,item) {

            var numLi = $("<li></li>").append($("<a></a>").append(item));
            if(result.data.pageNum==item){
                numLi.addClass("active");
            }
            numLi.click(function(){
                to_page(item,road);
            });
            ul.append(numLi);
        });
        //添加下一页和末页的提示
        ul.append(nextPageLi).append(lastPageLi);
        //把ul加入到nav
        var navEle = $("<nav></nav>").append(ul);
        navEle.appendTo("#page_nav_area");
    }


    //通过审核
    $(document).on("click",".sub_btn",function() {
        // var val = $(this).attr("id");
        check($(this).attr("id"), 1,"/updatePaper");


    });
    //未通过审核
    $(document).on("click",".fail_btn",function() {
        // var val = $(this).attr("id");
        check($(this).attr("id"), 2,"/updatePaper");
    });

    function check(paperId, opeCode,road){
        // alert(paperId);
        $.ajax({
            url:"${APP_PATH}"+road,
            data:{"paperId":paperId+"","operaCode":opeCode},
            type:"GET",
            success:function(result){
                /*//console.log(result);
                //1、解析并显示数据
                build_check_paper(result);
                //2、解析并显示分页数据
                build_page_info(result);
                //3、解析显示分页条数据
                build_page_nav(result,road);*/

                to_page(1,"/selAllNewPaper");

            }
        });

    }
</script>

<jsp:include page="checker_foot.jsp"/>
</body>
</html>
